/**
 *	\file AKDSQImageProvider.m
 *  
 *  \section desc_sec Description
 *  Description
 *  
 *  \if copyright
 *  	Copyright 2008 Ben Chen. All rights reserved.
 *  \endif
 *  
 *  \section hist_sec Change History:
 *  	-	2/21/08		BC		Created this file
 *  
 *  <hr>
 *  \addtogroup AnimalKingdom
 *  @{
 */

#import "AKDSQ.h"
#import "AKDSQImageProvider.h"

@implementation AKDSQImageProvider

- (id) init {
	self = [super init];
	if (self != nil) {
		names = [[NSArray alloc] initWithObjects:@"Rat", @"Dog", @"Snake", @"Sheep", @"Tiger", @"Lion", "Elephant", nil];
	}
	return self;
}

- (void) dealloc {
	[names release];
	[super dealloc];
}

- (NSString*)imageNameForPiece:(AKPiece)piece isReflection:(BOOL)flag {	
	if (piece.piece >= AKDSQPiecesCount)
		[[NSException exceptionWithName:NSInvalidArgumentException 
								 reason:[NSString stringWithFormat:@"imageNameForPiece: piece index:%d out of bound (%d)", piece.piece, AKDSQPiecesCount]
							   userInfo:nil] raise];
	if (piece.side >= AKDSQSidesCount)
		[[NSException exceptionWithName:NSInvalidArgumentException
								 reason:[NSString stringWithFormat:@"imageNameForPiece: side index:%d out of bound (%d)", piece.side, AKDSQSidesCount]
							   userInfo:nil] raise];
	
	NSString *colorStr	= (piece.side == 0 ? @"W" : @"B");
	NSString *flagStr	= (flag == NO ? @"0" : @"1");
	
	return [NSString stringWithFormat:@"AK_%@_%@_%@", names[piece.piece], colorStr, flagStr];
}

- (CGImageRef)imageRefWithName:(NSString*)fileName type:(NSString*)type {
	CGImageRef retVal;
	
	CGImageSourceRef source;
	source = CGImageSourceCreateWithURL((CFURLRef)[NSURL fileURLWithPath:
												   [[NSBundle mainBundle] pathForResource:fileName ofType:type]], NULL);
	retVal = CGImageSourceCreateImageAtIndex(source, 0, NULL);
	CFRelease(source);
	
	[(id)retVal autorelease];
	return retVal;	
}

- (CGImageRef)imageForPiece:(AKPiece)piece {
	return [self imageRefWithName:[self imageNameForPiece:piece isReflection:NO] type:@"png"]; 
}

- (CGImageRef)reflectionForPiece:(AKPiece)piece {
	return [self imageRefWithName:[self imageNameForPiece:piece isReflection:YES] type:@"png"]; 
}

- (CGImageRef)backgroundImage {
	return [self imageRefWithName:@"AK_Background" type:@"jpg"];
}

- (CGImageRef)preview {
	// TODO:
	return nil;
}

@end

/** @} */
